<?php

require_once __DIR__ . '/functions.php';

use Web3\Contract;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Web3;
use Web3p\EthereumTx\Transaction;

$abi = '[
                                {
                                  "constant": true,
                                  "inputs": [],
                                  "name": "name",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "string"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "constant": false,
                                  "inputs": [
                                    {
                                      "name": "_spender",
                                      "type": "address"
                                    },
                                    {
                                      "name": "_value",
                                      "type": "uint256"
                                    }
                                  ],
                                  "name": "approve",
                                  "outputs": [
                                    {
                                      "name": "success",
                                      "type": "bool"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "nonpayable",
                                  "type": "function"
                                },
                                {
                                  "constant": true,
                                  "inputs": [],
                                  "name": "totalSupply",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "uint256"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "constant": false,
                                  "inputs": [
                                    {
                                      "name": "_from",
                                      "type": "address"
                                    },
                                    {
                                      "name": "_to",
                                      "type": "address"
                                    },
                                    {
                                      "name": "_value",
                                      "type": "uint256"
                                    }
                                  ],
                                  "name": "transferFrom",
                                  "outputs": [
                                    {
                                      "name": "success",
                                      "type": "bool"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "nonpayable",
                                  "type": "function"
                                },
                                {
                                  "constant": true,
                                  "inputs": [],
                                  "name": "decimals",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "uint8"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "constant": true,
                                  "inputs": [],
                                  "name": "standard",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "string"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "constant": true,
                                  "inputs": [
                                    {
                                      "name": "",
                                      "type": "address"
                                    }
                                  ],
                                  "name": "balanceOf",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "uint256"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "constant": true,
                                  "inputs": [],
                                  "name": "symbol",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "string"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "constant": false,
                                  "inputs": [
                                    {
                                      "name": "_to",
                                      "type": "address"
                                    },
                                    {
                                      "name": "_value",
                                      "type": "uint256"
                                    }
                                  ],
                                  "name": "transfer",
                                  "outputs": [],
                                  "payable": false,
                                  "stateMutability": "nonpayable",
                                  "type": "function"
                                },
                                {
                                  "constant": true,
                                  "inputs": [
                                    {
                                      "name": "",
                                      "type": "address"
                                    },
                                    {
                                      "name": "",
                                      "type": "address"
                                    }
                                  ],
                                  "name": "allowance",
                                  "outputs": [
                                    {
                                      "name": "",
                                      "type": "uint256"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "view",
                                  "type": "function"
                                },
                                {
                                  "inputs": [
                                    {
                                      "name": "initialSupply",
                                      "type": "uint256"
                                    },
                                    {
                                      "name": "tokenName",
                                      "type": "string"
                                    },
                                    {
                                      "name": "decimalUnits",
                                      "type": "uint8"
                                    },
                                    {
                                      "name": "tokenSymbol",
                                      "type": "string"
                                    }
                                  ],
                                  "payable": false,
                                  "stateMutability": "nonpayable",
                                  "type": "constructor"
                                },
                                {
                                  "anonymous": false,
                                  "inputs": [
                                    {
                                      "indexed": true,
                                      "name": "from",
                                      "type": "address"
                                    },
                                    {
                                      "indexed": true,
                                      "name": "to",
                                      "type": "address"
                                    },
                                    {
                                      "indexed": false,
                                      "name": "value",
                                      "type": "uint256"
                                    }
                                  ],
                                  "name": "Transfer",
                                  "type": "event"
                                },
                                {
                                  "anonymous": false,
                                  "inputs": [
                                    {
                                      "indexed": true,
                                      "name": "_owner",
                                      "type": "address"
                                    },
                                    {
                                      "indexed": true,
                                      "name": "_spender",
                                      "type": "address"
                                    },
                                    {
                                      "indexed": false,
                                      "name": "_value",
                                      "type": "uint256"
                                    }
                                  ],
                                  "name": "Approval",
                                  "type": "event"
                                }
                            ]';

// 发起转移用户 address 和 private key
#$from_addr = '0x9d929d0802580915054e019939f66d2917d9261a';
#$from_key = '2c13d0243268294ba44150d6422676e4152bf4cf03a4483468a49db19abe3226';

// 发起转移用户 address 和 private key
$from_addr = '0xB435784C649Dd199B109976681D60A8ccdFF48f0';

// 接收用户 address
$to_addr = '0x7410b44be4c56d89be09833647f6e92ebd4342ae';

// 合约 address
$contract_addr = '0xfd2a7754c241624bf48b2bc3a160b9a2f309355e';
$gasLimit = '0x' . dechex(150000);
$gasPrice = '0x83156a3e07';
// 转移NFT token id
$tokenId = '0x' . dechex(1);

// 获取from account 的交易数量
$params = [
    $from_addr,
    'latest',
];
$json = httpPost('scs_getTransactionCount', $params);
$nonce = $json['result'];

$url = 'https://backend.straitchain.com/webclient/api/develop/straits/action';
//$url = 'https://ropsten.infura.io/v3/8f762549c8c341388ac03552835a0358';
//$url = 'https://kovan.infura.io/v3/8f762549c8c341388ac03552835a0358';
$web3 = new Web3(new HttpProvider(new HttpRequestManager($url, 15)));
$contract = new Contract($web3->getProvider(), $abi);

// transferFrom data
$data = $contract->at($contract_addr)->getData('transferFrom', $from_addr, $to_addr, $tokenId);

$trans = [
    "from"     => $from_addr,
    "to"       => $to_addr,
    "data"     => '0x' . $data,
    'gas'      => $gasLimit,
    'gasPrice' => $gasPrice,
    'nonce'    => $nonce,
];

$tx = new Transaction($trans);
$from_key = 'e9b81ed0c48a07da142882f0cedff5af4567d20198169a7288eb962d52270639';
$raw = $tx->sign($from_key);
$json = httpPost('scs_sendRawTransaction', ['0x' . $raw]);
var_dump($json);